package com.example.restaurantfinder.utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restaurantfinder.config.Configuration;
import com.example.restaurantfinder.model.Restaurant;
import com.example.restaurantfinder.model.RestaurantList;
import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.jackson.JacksonFactory;


public class Processor {
	
private static final int BUFFER_SIZE = 512;
private static RestaurantList list = new RestaurantList(); 
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

public static double latitude;
public static double longitude;
public static String types;
public static String name;
public static double radius;
private String lati;
private String longi;
	
	public static RestaurantList getData(String urlString){
		String eventList = "";

		
		
		/*LocationManager mlocManager=null;  
         LocationListener mlocListener;  
         //mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
         
         mlocListener = new GPS();  
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);  
  
        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {  
            if(GPS.lati>0)  
            {  
            	//GPSTv.setText("Latitude:- " + GPS.latitude +"Longitude:- " + GPS.longitude );
            	latitude = (GPS.lati);
            	longitude = (GPS.longi);
             }  
             else  
             {  
            	 
            	 //Toast.makeText(this, "Enable GPS", Toast.LENGTH_SHORT).show();
            	 
              }  
          } else {  
        	  System.out.println("GPS is not turned on...");  
          }  */
	
		 HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
         HttpRequest request;
		try {
			request = httpRequestFactory
			         .buildGetRequest(new GenericUrl(Configuration.googlePlacesLink));
			  request.getUrl().put("key", "AIzaSyDIXIvIZfCclEPPC_Rc1kF7ZcBD42pB3bk");
			  request.getUrl().put("location", latitude + "," + longitude);
			  request.getUrl().put("radius", radius);
			  request.getUrl().put("types", types);
			  request.getUrl().put("name", name);
			 
			  
			  list = request.execute().parseAs(RestaurantList.class);
			  return list;
			  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
     //    request.getUrl().put("reference", reference);
       //  request.getUrl().put("sensor", "false");
		return list;
	}
	
	 public static HttpRequestFactory createRequestFactory(
	            final HttpTransport transport) {
	        return transport.createRequestFactory(new HttpRequestInitializer() {
	            public void initialize(HttpRequest request) {
	                GoogleHeaders headers = new GoogleHeaders();
	                headers.setApplicationName("My Places");
	                request.setHeaders(headers);
	                JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
	                request.addParser(parser);
	            }
	        });
	    }
	
	

}
